package com.ruoyi.system.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Locale;

/**
 * @author Administrator
 */
@Component
public class MsgUtil implements ApplicationContextAware {

    private String from="1354742382@qq.com";

    @Autowired
    private JavaMailSender sender;

    public void sendTextEmail(String to,String subject,String content){
                 SimpleMailMessage message = new SimpleMailMessage();
                 message.setFrom(from);
                 message.setTo(to);
                 message.setSubject(subject);
                 message.setText(content);
                 message.setSentDate(new Date());
                 sender.send(message);
             }


    private static ApplicationContext context;

    @Override
    @SuppressWarnings("static-access" )
    public void setApplicationContext(ApplicationContext contex)
            throws BeansException {
        // TODO Auto-generated method stub
        this.context = contex;
    }
    public static Object getBean(String beanName){
        return context.getBean(beanName);
    }

    public static String getMessage(String key){
        return context.getMessage(key, null, Locale.getDefault());
    }
}
